package com.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.model.good.Good;
import com.model.indent.Indent;

/**
 * 分页dao层
 * @author 飘仙人
 *
 */
public class PagingDao {
	private StringBuffer sql = null;
	
	/**
	 * 获取总记录数
	 * @param table
	 * @return
	 * @throws Exception
	 */
	public int getTotalrows(String table) throws Exception{
		int iFlag = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("select count(*) from ").append(table).append(";");
		
		ResultSet rs = DBHelper.queryData(sql.toString());
		
		if(rs.next()){
			iFlag = rs.getInt(1); 
		}
		return iFlag;
	}
	
	/**
	 * 获取页面数据（商品）
	 * @param table
	 * @param fromIndex
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List<Good> getGoodPagedata(String table, int fromIndex,int pageSize) throws Exception{
		sql = new StringBuffer("");
		sql.append("select * from ").append(table).append(" limit ?,?");
		
		PreparedStatement pstmt = DBHelper.getConn().prepareStatement(sql.toString());
		pstmt.setInt(1, fromIndex); 
		pstmt.setInt(2, pageSize); 
		
		ResultSet rs = pstmt.executeQuery();

		List<Good> goodList = new ArrayList<Good>();
		while(rs.next()){
			Good g = new Good();
			
			g.setId(rs.getLong("id"));
			g.setGoodname(rs.getString("goodname"));
			g.setScore(rs.getInt("score"));
			g.setPrice(rs.getDouble("price"));
			g.setMemberprice(rs.getDouble("memberprice"));
			g.setSolded(rs.getInt("solded"));
			g.setAddate(rs.getDate("addate"));
			g.setNum(rs.getInt("num"));
			g.setPicurl(rs.getString("picurl"));
			
			goodList.add(g);
		}	
		return goodList;	
	}
	
	/**
	 * 获取页面数据（订单）
	 * @param table
	 * @param fromIndex
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	public List<Indent> getIndentPagedata(String table, int fromIndex,int pageSize) throws Exception{
		sql = new StringBuffer("");
		sql.append("select * from ").append(table).append(" limit ?,?");
		
		PreparedStatement pstmt = DBHelper.getConn().prepareStatement(sql.toString());
		pstmt.setInt(1, fromIndex); 
		pstmt.setInt(2, pageSize); 
		
		ResultSet rs = pstmt.executeQuery();

		List<Indent> indentList = new ArrayList<Indent>();
		while(rs.next()){
			Indent i = new Indent();
			
			i.setActionid(rs.getLong("actionid"));
			i.setUsername(rs.getString("username"));
			i.setGoodname(rs.getString("goodname"));
			i.setProductnum(rs.getInt("productnum"));
			i.setActiondate(rs.getDate("actiondate"));
			i.setState(rs.getString("state"));
			i.setRelname(rs.getString("relname"));
			i.setMobile(rs.getString("mobile"));
			i.setAddress(rs.getString("address"));
			i.setPostcode(rs.getString("postcode"));
			i.setPrice(rs.getDouble("price"));
			i.setMemberprice(rs.getDouble("memberprice"));
			i.setTotal(rs.getDouble("total"));
			i.setPicurl(rs.getString("picurl"));
			
			indentList.add(i);
		}	
		return indentList;	
	}
}